package org.jhulk.utils;

import org.jhulk.sprites.Marine;
import org.jhulk.sprites.Squad;
import org.jhulk.states.GameCache;
import org.newdawn.slick.XMLPackedSheet;

public class GameUtils 
{
	/**
	 * Given a future set of x y coords, loop through all characters
	 * and determine if any have matching coords. Return true if we find any matches
	 * @param x
	 * @param y
	 * @return
	 */
	public static boolean detectCollision(int x, int y, GameCache cache)
	{
		boolean collision = false;
		
		for(Squad s : cache.getSquads())
    	{
    		for(Marine m : s.getAllUnits())
    		{
    			if(m.getX() == x && m.getY() == y)
    				collision = true;
    		}
    	}
		
		return collision;
	}
	

    /**
     * MOVE THIS TO UTILS, ITS NOT A SPRITE UTILITY
     * Takes a squad string, parses it and returns a populated squad object;
     * Squad string syntax:
     * m == marine : number of units
     * f == flamer : number of units
     * s ==  sergeant : number of units
     * ex. m:2,f:1,s:1 (2 marines, 1 flamer, 1 sergeant)
     * @param ss
     * @return
     */
    public static Squad parseSquadString(String ss, XMLPackedSheet sheet)
    {
        Squad retSquad = new Squad();

        int numMarines, numFlamers, numSergeants;

        System.out.println("Initializing squad: " + ss);

        String[] splitSquad = ss.split(",");

        for(int i=0; i < splitSquad.length; i++)
        {
            //Determine the number of marines and initialize them
            if(splitSquad[i].charAt(0) == 'm')
            {
                String[] splitMarine = splitSquad[i].split(":");

                try
                {
                    numMarines = Integer.parseInt(splitMarine[1]);
                    for(int m=0; m < numMarines; m++)
                    {
                        retSquad.addMarine(new Marine(sheet.getSprite("marine_42.png"), "Pasanius" + m));
                        System.out.println("Initialized marine: Pasanius" + m );
                    }

                } catch(NumberFormatException nfe) {
                    System.out.println("Invalid number of marines!");
                } catch(Exception e){
                    e.printStackTrace();
                }
            }

            //Determine the number of flamers and initialize them
            if(splitSquad[i].charAt(0) == 'f')
            {
                String[] splitFlamers = splitSquad[i].split(":");

                try
                {
                    numFlamers = Integer.parseInt(splitFlamers[1]);
                    for(int m=0; m < numFlamers; m++)
                    {
                        retSquad.addFlamer(new Marine(sheet.getSprite("flamer_42.png"), "Collix" + m));
                        System.out.println("Initialized Flamer: Collix" + m );
                    }

                } catch(NumberFormatException nfe) {
                    System.out.println("Invalid number of flamers!");
                } catch(Exception e){
                    e.printStackTrace();
                }
            }

            //Determine the number of sergeants and initialize them
            if(splitSquad[i].charAt(0) == 's')
            {
                String[] splitSergeants = splitSquad[i].split(":");

                try
                {
                    numSergeants = Integer.parseInt(splitSergeants[1]);
                    for(int m=0; m < numSergeants; m++)
                    {
                        retSquad.addFlamer(new Marine(sheet.getSprite("sergeant_42.png"), "Lerchus" + m));
                        System.out.println("Initialized Sergeant: Lerchus" + m );
                    }

                } catch(NumberFormatException nfe) {
                    System.out.println("Invalid number of sergeants!");
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        }

        return retSquad;
    }
	
	
}
